home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 January / PSL Monthly Shareware CD-ROM (Public Software Library) (January 1994).iso / games / dos / board / backgam.com / MOVE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1979-11-30  |  5.1 KB  |  232 lines

  1. /*P*/
  2. /****************************************************************/
  3. /*                                */
  4. /* move piece                            */
  5. /*                                */
  6. /* Move_piece takes care of decrementing and incrementing the    */
  7. /* board array, and calling a function to display changes in    */
  8. /* the playing status. There are some checks made to see if    */
  9. /* source and destination are within range - these need to be    */
  10. /* here, and they're not a bug trap or anything. We can move    */
  11. /* pieces FROM an "out-of-range" source when we initialize the    */
  12. /* playing surface. We can move pieces TO an out-of-range dest    */
  13. /* when we take them off the board at the end (23/5 move, as    */
  14. /* an example for black).                    */
  15. /*                                */
  16. /****************************************************************/
  17.  
  18. #include    <stdio.h>
  19. #include    "bkg.h"
  20.  
  21. #define        BLINK        0x80
  22.  
  23. move_piece( old_psn, new_psn, who, side )
  24.  
  25. int    old_psn, new_psn, who, side;
  26.  
  27.     {
  28.  
  29.     char    name[ 2 ], color[ 2 ];
  30.  
  31. #ifdef    KAYPRO
  32.  
  33.     name[ 0 ] = 'W';
  34.     name[ 1 ] = 'B';
  35.  
  36. #endif
  37.  
  38. #ifdef    IBMPC
  39.  
  40.     name[ 0 ] = 0x02;    /* white */
  41.     name[ 1 ] = 0x01;    /* black */
  42.  
  43.     color[ 0 ] = 0x07;    /* white */
  44.     color[ 1 ] = 0x06;    /* black */
  45.  
  46. #endif
  47.  
  48. #ifdef    VAX
  49.  
  50.     name[ 0 ] = 'W';
  51.     name[ 1 ] = 'B';
  52.  
  53. #endif
  54.  
  55.     move_it( old_psn, who, ' ', 0x00, side );
  56.  
  57.     /* move them puppies!    */
  58.  
  59.     if ( ( old_psn >= 0  ) &&
  60.          ( old_psn <= 25 ) )
  61.         board[ old_psn ][ who ]--;
  62.  
  63.     if ( ( ( who == BLACK  ) &&
  64.            ( new_psn < 25  ) ) ||
  65.          ( ( who == WHITE  ) &&
  66.            ( new_psn > 0   ) ) )
  67.         board[ new_psn ][ who ]++;
  68.  
  69.     move_it( new_psn, who, name[ who ], color[ who ], side );
  70.  
  71.     } /* move piece */
  72.  
  73. /*P*/
  74. /****************************************************************/
  75. /*                                */
  76. /* move_it                            */
  77. /*                                */
  78. /* This is the function to actually place a piece on the board.    */
  79. /* We divide the board into quadrants in this function, from 1    */
  80. /* being white's inner table, to 4 being black's inner table.    */
  81. /* Quadrants 0 and 5 are the bar. If we view the board from    */
  82. /* white's vantage point, the values of the point are reversed    */
  83. /* before calculating the quadrant. The variable "old_psn" is    */
  84. /* not aptly named in all cases; it is the point at which data    */
  85. /* are written, and this can be a blank for removing data, or    */
  86. /* a black/white man for adding data. Lastly, note that on the    */
  87. /* main points, we can fit 15 min in 3 cols of 5. On the bar,    */
  88. /* we can fit 3 rows of 3. So we scale the row/col number to    */
  89. /* write the data at the correct spot on the display.        */
  90. /*                                */
  91. /****************************************************************/
  92.  
  93. move_it( old_psn, who, data, color, side )
  94.  
  95. int    old_psn, who;
  96. char    data;
  97. int    color;
  98. int    side;
  99.  
  100.     {
  101.  
  102.     int    quad, x, y, disp;
  103.     int    test, limit;
  104.     
  105.     /* if the old position is in range,    */
  106.     /* move the piece there.        */
  107.  
  108.     if ( ( ( who == WHITE  ) &&
  109.            ( old_psn > 0   ) &&
  110.            ( old_psn <= 25 ) ) ||
  111.          ( ( who == BLACK  ) &&
  112.            ( old_psn >= 0  ) &&
  113.            ( old_psn < 25  ) ) )
  114.         {
  115.  
  116.         test = old_psn;
  117.         limit = 5;
  118.  
  119.         if ( side )
  120.             {
  121.  
  122.             if ( old_psn == 25 )
  123.                 old_psn = 0;
  124.             else
  125.                 if ( old_psn == 0 )
  126.                     old_psn = 25;
  127.                 else
  128.                     if ( old_psn < 13 )
  129.                         old_psn = old_psn + 12;
  130.                     else
  131.                         old_psn = old_psn - 12;
  132.  
  133.             } /* white side */
  134.  
  135.         /* Which quadrant are we in?    */
  136.         /* The bar's are different.    */
  137.  
  138.         if ( old_psn == 0 )
  139.             quad = 4;
  140.         else
  141.             if ( old_psn == 25 )
  142.                 quad = 5;
  143.             else
  144.                 quad = ( old_psn - 1 ) / 6;
  145.  
  146.         switch( quad )
  147.             {
  148.  
  149.             case 0:        /* white's inner table */
  150.                     x = 2;
  151.                     y = ( old_psn * 4 ) - 1;
  152.                     break;
  153.  
  154.             case 1:        /* white's outer table */
  155.                     x = 2;
  156.                     y = ( old_psn * 4 ) + 7;
  157.                     break;
  158.  
  159.             case 2:        /* black's outer table */
  160.                     x = 14;
  161.                     y = ( ( 25 - old_psn ) * 4 ) + 7;
  162.                     break;
  163.  
  164.             case 3:        /* black's inner table */
  165.                     x = 14;
  166.                     y = ( ( 25 - old_psn ) * 4 ) - 1;
  167.                     break;
  168.  
  169.             case 4:        /* black's bar */
  170.                     x = 5;
  171.                     y = 29;
  172.                     limit = 2;
  173.                     color |= BLINK;
  174.                     break;
  175.  
  176.             case 5:        /* white's bar */
  177.                     x = 11;
  178.                     y = 29;
  179.                     limit = 2;
  180.                     color |= BLINK;
  181.                     break;
  182.  
  183.             default:    printf( "move_piece: logic error #1: %d\n", quad );
  184.                     exit();
  185.                     break;
  186.  
  187.             } /* switch */
  188.  
  189.         /* Only actually place the piece if    */
  190.         /* there is room for it. This is 15 on    */
  191.         /* a point (so we are guaranteed) but    */
  192.         /* only 9 on the bar.            */
  193.  
  194.         if ( board[ test ][ who ] <= ( limit * 3 ) )
  195.             {
  196.  
  197.             /* offset the column by 1 either way    */
  198.             /* according to the # of pieces there.    */
  199.  
  200.             if ( board[ test ][ who ] > limit )
  201.                 if ( board[ old_psn ][ who ] > ( limit * 2 ) )
  202.                     y++;
  203.                 else
  204.                     y--;
  205.  
  206.             /* offset the row according to the #    */
  207.             /* of pieces mod 5 that are there.    */
  208.  
  209.             disp = board[ test ][ who ];
  210.             while( disp > limit )
  211.                 disp -= limit;
  212.  
  213.             if ( ( quad < 2  ) ||
  214.                  ( quad == 4 ) )
  215.                 x += disp - 1;
  216.             else
  217.                 x -= disp - 1;
  218.  
  219.             if ( debug > 3 )
  220.                 {
  221.                 char_at( x, y, '+', 0x01 );
  222.                 raw_keyboard();
  223.                 }
  224.  
  225.             char_at( x, y, data, color );
  226.  
  227.             } /* within 15 or 9 */
  228.  
  229.         } /* old psn in range */
  230.  
  231.     } /* move_it */
  232.